#include "GLAllInOne.h"
#include "GameObject.h"
#include "Transform.h"
#include "Mesh.h"
#include "Texture.h"
#include "Shader.h"
#include "Camera.h"
#include "Renderer.h"
namespace ManchyGL
{
	Renderer::Renderer()
	{}

	Renderer::~Renderer()
	{
	}

	void Renderer::Render(Camera* camera)
	{
		if (NULL == mesh)
			return;
		if (NULL == shader)
			return;

		/*
		if (NULL == texture1)
			return;
		if (NULL == texture2)
			return;
		*/
		
		GameObject* go = this->GetGameObject();
		glm::mat4 model = go->GetTransform()->GetMatrix();
		glm::mat4 view = camera->GetViewMatrix();
		glm::mat4 projection = camera->GetProjectionMatrix();
		glm::mat4 transform = projection * view * model;
		shader->SetFloat("mixValue", 0.2f);
		shader->SetMat4("transform", transform);

		//shader
		glEnable(GL_DEPTH_TEST);
		shader->Use();

		if (NULL != texture1)
		{
			glActiveTexture(GL_TEXTURE0);
			glBindTexture(GL_TEXTURE_2D, texture1->GetTextureId());
			glUniform1i(shader->GetPropertyId("texture1"), 0);
		}

		if (NULL != texture2)
		{
			glActiveTexture(GL_TEXTURE0 + 1);
			glBindTexture(GL_TEXTURE_2D, texture2->GetTextureId());
			glUniform1i(shader->GetPropertyId("texture2"), 1);
		}
		

		glBindVertexArray(mesh->GetVAO());
		if (mesh->GetUseEBO())
		{
			glDrawElements(GL_TRIANGLES, mesh->GetTriNum(), GL_UNSIGNED_INT, 0);
		}
		else
		{
			glDrawArrays(GL_TRIANGLES, 0, mesh->GetTriNum());
		}
	}

}